package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description: 处理相册数据的控制器类
 * @Author: sweeterjava@163.com
 * @Date: 2022/10/26
 * @Time: 15:41
 */

@Api(tags = "02.品牌管理")
@Slf4j
@RestController
@RequestMapping("/brands")
public class BrandController {
    @Autowired
    private IBrandService brandService;

    public BrandController() {
        log.debug("创建控制器对象：BrandController");
    }

    @ApiOperation("添加新品牌")
    @ApiOperationSupport(order = 101)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid BrandAddNewDTO brandAddNewDTO) {
        log.debug("开始处理【添加新品牌】请求：{}", brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        log.debug("处理【添加新品牌】请求成功");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除属性模版")
    @ApiOperationSupport(order = 201)
    @PostMapping("/{id:[0-9]+}/delete")
    public String delete(@PathVariable Long id) {
        String message = "尝试删除id为" + id + "的属性模版";
        log.debug(message);
        return message;
    }

    @ApiOperation("【已过期】根据name删除属性模版")
    @ApiOperationSupport(order = 901)
    @PostMapping("/{id:[a-z]+}/delete2")
    public String delete2(@PathVariable String name) {
        String message = "尝试删除名为" + name + "的属性模版";
        log.debug(message);
        return message;
    }

    @ApiOperation("品牌列表")
    @ApiOperationSupport(order = 310)
    @GetMapping("")
    public JsonResult<List<BrandListItemVO>> list() {
        log.debug("开始处理品牌列表请求");
        return JsonResult.ok(brandService.list());
    }

    @ApiOperation("启用品牌")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> SetEnable(@Range(min = 1, message = "启用品牌失败，参数错误")
                                      @PathVariable Long id) {
        log.debug("开始处理【启用品牌】的请求，参数：{}", id);
        brandService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用品牌")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> SetDisable(@Range(min = 1, message = "禁用品牌失败，参数错误")
                                       @PathVariable Long id) {
        log.debug("开始处理【禁用品牌】的请求，参数：{}", id);
        brandService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/9527
    @ApiOperation("根据id查询品牌详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<BrandStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询品牌详情】的请求，参数：{}", id);
        BrandStandardVO brand = brandService.getStandardById(id);
        return JsonResult.ok(brand);
    }

    @ApiOperation("重建品牌缓存")
    @ApiOperationSupport(order = 600)
    @PostMapping("/cache/rebuild")
    public JsonResult<Void> rebuildCache() {
        log.debug("开始处理【重建缓存】的请求");
        brandService.rebuildCache();
        return JsonResult.ok();
    }



}
